﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace FPSO.Models.ViewModel.Global
{
    /// <summary>
    /// 全局数据源
    /// </summary>
    public class GlobalData
    {
        /// <summary>
        /// 全局单例，保证数据唯一
        /// </summary>
        private static readonly Lazy<GlobalData> _instance = new Lazy<GlobalData>(() => new GlobalData());

        /// <summary>
        /// 全局访问入口
        /// 从任何地方都可以通过DataList.Instance访问到数据
        /// </summary>
        public static GlobalData Instance => _instance.Value;

        /// <summary>
        /// 数据集合字典，所有数据都在这里
        /// </summary>
        private Dictionary<string, ObservableCollection<BaseEntity>> _dataSets;

        /// <summary>
        /// 添加新数据集合
        /// </summary>
        /// <param name="key">数据集合名称</param>
        /// <param name="dataSet">数据集合</param>
        public void AddDataSet(string key, ObservableCollection<BaseEntity> dataSet)
        {
            if (!_dataSets.ContainsKey(key))
            {
                _dataSets[key] = dataSet;
            }
        }

        /// <summary>
        /// 获取指定数据集合
        /// </summary>
        /// <param name="key">数据集合名称</param>
        /// <returns></returns>
        public ObservableCollection<BaseEntity> GetDataSet(string key)
        {
            return _dataSets.ContainsKey(key) ? _dataSets[key] : null;
        }

        /// <summary>
        /// 删除指定数据集合
        /// </summary>
        /// <param name="key">数据集合名称</param>
        public void RemoveDataSet(string key)
        {
            _dataSets.Remove(key);
        }

        /// <summary>
        /// 添加数据到指定集合
        /// </summary>
        /// <param name="key">数据集合名称</param>
        /// <param name="newItem">单条数据项</param>
        public void AddToDataSet(string key, BaseEntity newItem)
        {
            if (_dataSets.ContainsKey(key))
            {
                _dataSets[key].Add(newItem);
            }
        }

        /// <summary>
        /// 从指定集合中删除数据
        /// </summary>
        /// <param name="key">数据集合名称</param>
        /// <param name="item">单条数据项</param>
        public void RemoveFromDataSet(string key, BaseEntity item)
        {
            if (_dataSets.ContainsKey(key))
            {
                _dataSets[key].Remove(item);
            }
        }

        /// <summary>
        /// 修改指定集合中指定索引的数据
        /// </summary>
        /// <param name="key">数据集合名称</param>
        /// <param name="index">数据索引值</param>
        /// <param name="value">新数据值</param>
        public void UpdateIndexFromDataSet(string key, int index, object value)
        {
            if (_dataSets.ContainsKey(key))
            {
                _dataSets[key][index].Value = value;
            }
        }

        /// <summary>
        /// 初始无参构造函数，可定义初始数据集
        /// </summary>
        public GlobalData()
        {
            // 新建一个空的测试数据集
            GlobalData.Instance.AddDataSet("TestDataOne", new ObservableCollection<BaseEntity>());

            // 新建一个非空的测试数据集
            GlobalData.Instance.AddDataSet("TestDataTwo", new ObservableCollection<BaseEntity>
            {
                new BaseEntity() {Name = "测点1",Value= 12.8 },
                new BaseEntity() {Name = "测点2",Value= 23.4 },
                new BaseEntity() {Name = "开关1",Value= 0 },
                new BaseEntity() {Name = "开关2",Value= 1 },
            });

            // 对指定数据集添加数据
            GlobalData.Instance.AddToDataSet("TestDataOne", new BaseEntity { Name = "阀门1", Value = 1 });

            // 修改指定数据集指定索引的值
            GlobalData.Instance.UpdateIndexFromDataSet("TestDataOne", 0, 0);

            // 在XAML中绑定数据示例：
            // <!-- Label 显示某个集合中索引为 0 的元素的 Name 属性 -->
            // <Label Content = "{Binding Source={x:Static local:DataList.Instance}, Path=TestDataOne[0].Name}" />
        }
    }
}
